<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Menu;
use App\Services\AdminCacheService;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class MenuController extends Controller
{
    private $adminCacheService;

    public function __construct(AdminCacheService $adminCacheService)
    {
        $this->adminCacheService = $adminCacheService;
    }

    public function list(){
        global  $connection;
        $data = [];
        $menus = Menu::query()->select(['id','title','authority','sort','href','icon','is_menu','pid','status','is_menu'])
            ->orderBy('sort','asc')->get();
        $res = [
            'code'=>0,
            'msg'=>'',
            'count'=>count($data),
            'data'=>$menus,
            'env'=>config('app.env'),//test
            'connection'=>$connection, //显示当前连接 test
        ];
        return ResponseUtil::json($res);
    }

    public function add(){
        DB::beginTransaction();
        try {
            $request = request();
            $data = $request->only('title','authority','pid','icon','href','sort','status','is_menu');
            $pid = data_get($data,'pid');
            $sort = data_get($data,'sort');
            if (!$pid) $data['pid'] = 0;
            if (!$sort) $data['sort'] = 0;
            $authority = data_get($data,'authority');
            if (!$authority) $data['authority'] = null;
            $data['updated_at'] = $data['created_at'] = now();
            Menu::query()->insert($data);
            $this->adminCacheService->removeAllCache();
            self::refreshPermission();
            DB::commit();
            return ResponseUtil::success();
        } catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function detail($id){
        $row = Menu::query() ->find($id);
        return ResponseUtil::success($row);
    }

    public function edit($id) {
        DB::beginTransaction();
        try {
            $data = request()->only('title','authority','pid','icon','href','sort','status','is_menu');
            $data['updated_at'] = now();
            $data['pid'] = (int)$data['pid'];
            $authority = data_get($data,'authority');
            if (!$authority) $data['authority'] = null;
            Menu::query()->where('id',$id)->update($data);
            $this->adminCacheService->removeAllCache();
            self::refreshPermission();
            return ResponseUtil::success();
        } catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function delete($id){
        DB::beginTransaction();
        try {
            Menu::query()->where('id',$id)->delete();
            $this->adminCacheService->removeAllCache();
            self::refreshPermission();
            return ResponseUtil::success();
        } catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
    }

    private function refreshPermission(){ //刷新权限表 , 使用菜单数据作为权限
        Schema::disableForeignKeyConstraints();
        DB::select("TRUNCATE permissions;");
        DB::select("insert into permissions SELECT id , pid ,title,authority as name,'admin' as guard_name,sort, created_at, updated_at FROM `menu`");
        Schema::enableForeignKeyConstraints();
    }

}
